package com.tz.init;

import com.tz.AppConfig;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by robin on 2016/8/5 0005.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class InitTable {

    @Autowired
    private LocalSessionFactoryBean localSessionFactoryBean;

    @Test
    public void createTables() {
        Configuration cfg = localSessionFactoryBean.getConfiguration();
        SchemaExport se = new SchemaExport(cfg);
//        第一个参:sql脚本是否输出到控制台
        //第二个参:sql脚本是否导入到数据库
        se.create(true, true);
    }
}
